<?php

namespace app\common\model\wechat\tralts;

use hiduoke\services\facade\Snowflake;
use hiduoke\services\facade\WechatService;
use think\Exception;

trait WechatCreateCoupon
{

    /**
     * 设置卡券
     * @param array $coupon 卡券内容
     * @return $this
     */
    public function setCoupon($coupon = [])
    {
        $this->coupon = array_merge($this->coupon, $coupon);
        return $this;
    }

    /**
     * 设置卡券名称
     * @param $name
     * @param int $type
     * @return $this
     */
    public function setName($name, $type = 1)
    {
        $this->coupon['name'] = $name;
        $this->coupon['type'] = $type;
        return $this;
    }

    /**
     * 设置卡券标签
     * @param $tag
     * @return $this
     */
    public function setTag($tag)
    {
        $this->coupon['tag'] = $tag;
        return $this;
    }

    /**
     * 设置卡券图标
     * @param $tag
     * @return $this
     */
    public function setIcon($tag)
    {
        $this->coupon['tag'] = $tag;
        return $this;
    }

    /**
     * 设置卡券价值
     * @param $price
     * @return $this
     */
    public function setPrice($price)
    {
        $this->coupon['price'] = $price;
        return $this;
    }

    /**
     * 设置卡券价值
     * @param $discount
     * @return $this
     */
    public function setDiscount($discount)
    {
        $this->coupon['discount'] = $discount;
        return $this;
    }

    /**
     * 设置卡券核销员Uid
     * @param $check_uid
     * @return $this
     */
    public function setCheckUid($check_uid)
    {
        $this->coupon['check_uid'] = $check_uid;
        return $this;
    }

    /**
     * 设置卡券核销员组Id
     * @param $check_group_id
     * @return $this
     */
    public function setCheckGroupUid($check_group_id)
    {
        $this->coupon['check_group_id'] = $check_group_id;
        return $this;
    }

    /**
     * 设置卡券自定义跳转地址
     * @param $url
     * @return $this
     */
    public function setUrl($url)
    {
        $this->coupon['url'] = $url;
        return $this;
    }

    /**
     * 设置卡券核销码
     * @param $key
     * @return $this
     */
    public function setKey($key)
    {
        $this->coupon['key'] = $key;
        return $this;
    }

    /**
     * 创建核销码
     * @param $type
     * @param $number
     * @return WechatCreateCoupon
     */
    public function crateKey($type, $number)
    {
        return $this->setKey(get_rand($type, $number));
    }

    /**
     * 设置卡券规则
     * @param $rule
     * @return $this
     */
    public function setRule($rule)
    {
        $this->coupon['rule'] = $rule;
        return $this;
    }

    /**
     * 设置卡券类型
     * @param $type 1 核销券  2 现金券 3 折扣券
     * @return $this
     */
    public function setType($type)
    {
        $this->coupon['type'] = $type;
        return $this;
    }

    /**
     * 自定义卡券类型名称
     * @param $type_name
     * @return $this
     */
    public function setTypeName($type_name)
    {
        $this->coupon['type_name'] = $type_name;
        return $this;
    }

    /**
     * 设置卡券个数
     * @param $number
     * @return $this
     */
    public function setNumber($number)
    {
        $this->coupon['number'] = $number;
        return $this;
    }

    /**
     * 设置卡券可用时间
     * @param int $start_time 生效时间 0默认随时生效
     * @param int $end_time 到期时间 0默认不限到期时间
     * @return $this
     */
    public function setTime($start_time = 0, $end_time = 0)
    {
        if ($end_time <= $start_time) {
            throw new Exception('开始时间不得小于结束时间', -2);
        }
        $this->coupon['end_time'] = $end_time;
        $this->coupon['start_time'] = $start_time;
        return $this;
    }

    /**
     * 设置卡券可用时间
     * @param int $start_day 发放后多少天后生效 0默认为当天生效
     * @param int $use_day 可以生效多少天 0默认为不限
     * @return $this
     * @throws Exception
     */
    public function setUseDay($start_day = 0, $use_day = 0)
    {
        $start_time = (86400 * $start_day) + time();
        $end_time = $use_day != 0 ? (86400 * $use_day) + $start_time : 0;

        if ($end_time <= $start_time) {
            throw new Exception('开始时间不得小于结束时间', -2);
        }
        return $this->setTime($start_time, $end_time);
    }

    /**
     * 设置卡券自定义配置项
     * @param array $config
     * @return $this
     */
    public function setConfig($config = [])
    {
        $this->coupon['config'] = $config;
        return $this;
    }

    /**
     * 卡券使用地点
     * @param $name
     * @param $address
     * @param $latitude
     * @param $longitude
     * @param string $phone
     * @return $this
     */
    public function setPlace($name, $address, $latitude, $longitude, $phone = "")
    {
        $this->coupon['place'] = array(
            'name'      => $name,
            'address'   => $address,
            'latitude'  => $latitude,
            'longitude' => $longitude,
            'phone'     => $phone
        );
        return $this;
    }


    /**
     * 创建卡券
     * @param int $user_id
     * @return mixed
     */
    public function sendCoupon($user_id = 0)
    {
        $user_id = $user_id ?: WechatService::getUser()['id'];
        $this->coupon['uid'] = $user_id;
        $this->coupon['addon'] = $this->addon;
        $this->coupon['plaid'] = $this->plaid;
        $this->coupon['type'] = $this->coupon['type'] ?? 1;
        $this->coupon['key'] = $this->coupon['key'] ?? get_rand();
        $this->coupon['end_time'] = $this->coupon['end_time'] < time() ? time() + 86400 * 30 : $this->coupon['end_time'];
        return $this->save($this->coupon);
    }
}